********************************************************************************
* 
* Table 3: Management Practices
* This file produces the statistics for this table, which we constructed
* manually from the output
*
********************************************************************************

clear
capture log close
set more off
program drop _all

log using table3_main.log, replace

*******
******* Settings and directories 
*******

* path to the WMS surveys for sample of US hospitals + acquirer + target
local fpath_wms_aha_acq_tgt = "..."

*****
***** Prepare data
*****

use `fpath_wms_aha_acq_tgt', clear

keep company_id own2 c_management sysid sys* tot_int

* indicator for original WMS survey
gen byte wms = regexm(company_id,"^US")

* drop system hosps in original survey
drop if wms & sys

* among system hosps in survey, restrict to acquirer & target facilities
drop if sys_other | sys_other2
drop sys_other sys_other2

* all obs now non-system WMS interviews or system acquirer/target interviews
assert (wms & !sys) | (!wms &(sys_acq|sys_tgt))

* for-profit indicator
gen forprofit = (own2=="Private, for profit") | sys
drop sys

* destring the sysid so that we can xtset on it
destring sysid, replace

bys sysid: egen f=count(c_management)

*** COLUMN 1 AND 3, ROWS 1A, 1B, 2
* average management scores for WMS, legacy, target
gen analysis_group = wms + 2*sys_acq + 3*sys_tgt
label define analysis_group ///
	1 "wms" 2 "sys_acq" 3 "sys_tgt"
label values analysis_group analysis_group
mean c_management, over(analysis_group)

* testing that acquirer =/= WMS
test _b[c.c_management@1bn.analysis_group]=_b[c.c_management@2.analysis_group]
* testing that target =/= WMS
test _b[c.c_management@1bn.analysis_group]=_b[c.c_management@3.analysis_group]
* testing that acquirer =/= target
test _b[c.c_management@2.analysis_group]=_b[c.c_management@3.analysis_group]

*** COLUMN 1 AND 3, ROW 1
* average management score for acquirer+target
gen byte sys_either = sys_acq | sys_tgt
mean c_management if sys_either

* this program will estimate the within-system standard deviation
* of management for:
* - WMS interviews
* - Acquirer
* - Target
program define est_sigma, eclass
	
	tempvar esample
	gen byte `esample' = 0
	
	local Nobs = 0
	
	foreach group in wms sys_acq sys_tgt {
		* run the random effects regression
		
		* need to include random effects at the system
		* ID level, but only in the WMS sample, b/c in the
		* other samples, there is no variation in system ID
		* and xtmixed gets upset
		if ("`group'"=="wms") {
			local extra "|| sysid:"
		}
		else {
			local extra ""
		}
		
		* use REML to get unbiased estimate of unit-level
		* sigma
		qui xtmixed c_management `extra' if `group', reml
		
		* xtmixed stores the sd(Residual) as its log
		* need to extract with _diparm, exp
		* this gives identical estimate and s.e. as in
		* xtmixed
		qui _diparm lnsig_e, exp
		
		qui replace `esample' = `esample' | e(sample)
		local Nobs = `Nobs' + e(N)
		
		* estimate of the unit level sigma
		local sig_e_`group' = r(est)
		* variance of the estimate (squared std error)
		local sig_e_`group'_var = r(se)^2
	}
	
	tempname b V
	
	* the sigma estimates become the betas of this estimator
	matrix `b' = (`sig_e_wms',`sig_e_sys_acq',`sig_e_sys_tgt')
	matrix colnames `b' = sig_e_wms sig_e_sys_acq sig_e_sys_tgt
	
	* assemble the variance-covariance matrix of the sigma estimates
	* because they are estimated on distinct subsamples, the off-
	* diagonals are zeroes
	matrix `V' = ///
		diag((`sig_e_wms_var',`sig_e_sys_acq_var',`sig_e_sys_tgt_var'))
	matrix colnames `V' = sig_e_wms sig_e_sys_acq sig_e_sys_tgt
	matrix rownames `V' = sig_e_wms sig_e_sys_acq sig_e_sys_tgt

	ereturn post `b' `V', ///
		obs(`Nobs') depname(c_management) esample(`esample')
	
	ereturn display
end

* COLUMN 2, ROW 1
* sigma of the merged system
xtmixed c_management if sys_acq | sys_tgt , reml

* COLUMN 2 ROWS 1A, 1B, 2
* sigma of acquirer, target, and within-chain wms
est_sigma

test sig_e_wms = sig_e_sys_acq
test sig_e_wms = sig_e_sys_tgt
test sig_e_sys_acq = sig_e_sys_tgt

* additional stats for the paper

* COLUMN 4
* number of system hospitals in analysis
count if sys_acq | sys_tgt

* number of acquirer hospitals in analysis
count if sys_acq

* number of target hospitals in analysis
count if sys_tgt

* number of WMS hospitals in analysis
count if wms

* COLUMN 4, ROW 2
* number of chains in WMS sample
distinct sysid if wms

* number of chains in WMS sample with multiple hospitals
egen temp_hosps = count(1), by(wms sysid)
distinct sysid if wms & temp_hosps>1
drop temp_hosps

* FOOTNOTE 10
* confirm similar results if we restrict WMS to for-profits
keep if forprofit

* how many for-profits in WMS?
count if forprofit & wms

mean c_management, over(analysis_group)
test _b[c.c_management@1bn.analysis_group]=_b[c.c_management@2.analysis_group]
test _b[c.c_management@1bn.analysis_group]=_b[c.c_management@3.analysis_group]

est_sigma
test sig_e_wms = sig_e_sys_acq
test sig_e_wms = sig_e_sys_tgt

log close
